<?php
namespace Home\Controller;
use Think\Controller;
class OrderController extends CommonController {
    public function index(){
        ini_set('memory_limit','2048M');
        $memberModel = D("member");
        $orderModel = D("order");
        $storeModel = D("store");
        if($this->sessionInfo['parent_id'] == 0) {
            $condOrderWhere['m.first_class'] = $this->sessionInfo['user_id'];
        }else{
            $condOrderWhere['m.second_class'] = $this->sessionInfo['user_id'];
        }

        $page = I('p') ? I('p') : '1';
        $pageSize = 20;

        $condOrderWhere['o.status'] = array('in','20,30,40');
        $resOrder = $orderModel->alias('o')
            ->join("ecm_member m on o.buyer_id = m.user_id")
            ->join("ecm_order_goods g on o.order_id = g.order_id")
            ->where($condOrderWhere)->count();


        $resOrderList = $orderModel->alias('o')
            ->join("ecm_member m on o.buyer_id = m.user_id")
            ->join("ecm_order_goods g on o.order_id = g.order_id")
            ->where($condOrderWhere)->page($page,$pageSize)
            ->order('o.add_time desc')->select();
        foreach($resOrderList as $key=>$value){
            if($value['fourth_class'] != 0){
                $info = $memberModel->where(array("user_id"=>$this->store_ids))->find();
                $resOrderList[$key]['extension_post'] = '店铺推广';
                $resOrderList[$key]['extension_phone'] = $info['phone_mob'];
                if($value['second_class'] != 0){
                    $info = $memberModel->where(array("user_id"=>$value['second_class']))->find();
                    $resOrderList[$key]['extension_post'] = '店铺推广';
                    $resOrderList[$key]['extension_phone'] = $info['phone_mob'];
                }
                if($value['third_class'] != 0){
                    $info = $memberModel->where(array("user_id"=>$value['third_class']))->find();
                    $resOrderList[$key]['extension_post'] = '员工推广';
                    $resOrderList[$key]['extension_phone'] = $info['phone_mob'];
                }
            }else{
                $info = $memberModel->where(array("user_id"=>$this->store_ids))->find();
                $resOrderList[$key]['extension_post'] = '店铺账号推广';
                $resOrderList[$key]['extension_phone'] = $info['phone_mob'];
            }
            if($value['second_class']){
                $resOrderList[$key]['s_name'] = $storeModel->where(array("store_id"=>$value['second_class']))->getField("store_name");
            }else{
                $resOrderList[$key]['s_name'] = $storeModel->where(array("store_id"=>$value['first_class']))->getField("store_name");
            }

        }
        $page = $this->getPage($resOrder,$page,$pageSize);
        $this->assign('list',$resOrderList);
        $this->assign('page',$page);
        $this->display();

    }
}